package Cain.pcxbook 
{
	import flash.events.Event;
	import org.cove.ape.Vector;
	
	/**
	 * 循序載入的CainLoader
	 * @author Cain 2010
	 */
	public dynamic class CainLoaderList extends Vector.<CainLoader>
	{
		private var list_num:int;
		private var ww:Number;
		private var hh:Number;
		private var bgColor:uint;
		private var bgAlpha:Number;
		private var urlList:Vector.<String>;
		private var className:String;
		private var now_load_id:int;
		/**
		 * 循序載入的CainLoader
		 * @param	_ww 寬
		 * @param	_hh 高
		 * @param	_bgcolor 底色
		 * @param	_alpha 底色透明度
		 * @param	_className loading物件
		 */
		public function CainLoaderList(_ww:Number = 100,_hh:Number = 100,_bgcolor:uint = 0,_alpha:Number = 0,_className:String = "") 
		{
			ww = _ww;
			hh = _hh;
			bgcolor = _bgcolor;
			bgAlpha = _alpha;
			className = _className; 
			super();
		}
		/**
		 * 設定載入網址陣列
		 * @param	_urlLast 陣列
		 */
		public function setUrlLast(_urlLast:Vector.<String>):void 
		{
			urlList = _urlLast;
			list_num = _urlLast.length;
			for (var i:int = 0; i < list_num; i++) 
			{
				var _loader:CainLoader = new CainLoader(ww, hh);
				_loader.loadMovie = className;
				push(_loader);
			}
		}
		/**
		 * 開始循序載入
		 */
		public function startLoad():void 
		{
			load(now_load_id);
		}
		/**
		 * 載入指定id
		 * @param	_id 序號
		 * @param	next 是否繼續載入下一個
		 */
		public function load(_id:int,next:Boolean = false):void 
		{
			if (_id < list_num) 
			{
				var _loader:CainLoader = this[_id];
				if (_loader.loaded == 0) 
				{
					if (next) 
					{
						_loader.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_a_COMPLETE);
					}
					_loader.load(urlList[_id]);
				}else 
				{
					//目前的已載入,跳下一個..
					if (next) 
					{
						reLoad();
					}
				}
			}else 
			{
				throw(new Error("CainLoaderList沒有這個id"));
			}
		}
		/**
		 * 暫停載入
		 */
		public function stopLoad():void 
		{
			var _loader:CainLoader = this[now_load_id];
			_loader.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, on_a_COMPLETE);
		}
		private function on_a_COMPLETE(e:Event):void 
		{
			e.target.removeEventListener(Event.COMPLETE, on_a_COMPLETE);
			reLoad();
		}
		private function reLoad():void 
		{
			now_load_id++;
			if (now_load_id < list_num) 
			{
				load(now_load_id);
			}
		}
	}

}